package Array;

//给一个升序数组，输出数组中绝对值后 有几个不同的数
//时间复杂度O(n),空间复杂度O(1)
//双指针
public class 升序数组查看多少个绝对值不同的数 {

    public static int getDifAbs(int[] nums){
        int len = nums.length;
        //数组长度为0或1的时候
        if(len<2){
            return len;
        }
        int i = 0;
        int j = len-1;
        int count = 0;
        while(i<j){
            if(i<len-1 && nums[i] == nums[i+1]){
                i++;
                continue;
            }
            if(j>0 && nums[j] == nums[j-1]){
                j--;
                continue;
            }
            //一正一负
            if(nums[i] + nums[j] == 0){
                i++;
                j--;
            }else if(nums[i] + nums[j] > 0){
                //后面大，
                j--;
            }else{
                //前面负数绝对值大
                i++;
            }
            count++;
        }
        if(i==j){
            count++;
        }
        return count;

    }

    public static int getDifAbs1(int[] nums){
        int len = nums.length;
        //数组长度为0或1的时候
        if(len<2){
            return len;
        }
        int i = 0;
        int j = len-1;
        int curNum = 0;
        int count = 0;
        if(nums[i]+nums[j]>0){
            curNum = Math.abs(nums[j]);
            j--;
        }else{
            curNum = Math.abs(nums[i]);
            i++;
        }
        while(i<=j){
            if(nums[i]+nums[j]>0){
                if(curNum == Math.abs(nums[j])){
                    count++;
                }
                curNum = Math.abs(nums[j]);
                j--;
            }else{
                if(curNum == Math.abs(nums[i])){
                    count++;
                }
                curNum = Math.abs(nums[i]);
                i++;
            }
        }
        return len-count;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{ -5,-4,-3,-3,-2,-2,0,0,2,2,9,9,9 }; //6
        int[] nums1 = new int[]{ -1,0,1,1,2,3,4,5,6,7 }; // 8
        int[] nums2 = new int[]{ -2,-1,0,1,2,3 };  // 4
        int[] nums3 = new int[]{ -2,-1,0,0,1,1,1,2,3,3 }; //4
        System.out.println(getDifAbs(nums));
        System.out.println(getDifAbs(nums1));
        System.out.println(getDifAbs(nums2));
        System.out.println(getDifAbs(nums3));
    }
}
